package com.hqyj.erpserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.erpserver.dao.entity.ProductMaterialReceipt;
import com.hqyj.erpserver.service.IProductMaterialReceiptService;
import com.hqyj.erpserver.util.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HQYJ
 * @since 2021-10-25
 */
@RestController
@CrossOrigin//允许跨域请求
@Api(tags = "成品收料单维护")  //启用swagger文档
@RequestMapping("/productMaterialReceipt")
public class ProductMaterialReceiptController {

    @Autowired
    private IProductMaterialReceiptService productMaterialReceiptService;

    @RequestMapping("/getProductMaterialReceipt")
    @ApiOperation(value = "分页查询成品收料单信息", notes = "按条件查询成品收料单信息，分页返回数据")
    public RestResult<ProductMaterialReceipt> getProductMaterialReceipt(
            @ApiParam(value = "分页的页码", required = true)
            @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "每页记录数", required = true)
            @RequestParam(defaultValue = "2") Integer rows,
            @ApiParam(value = "查询条件") ProductMaterialReceipt productMaterialReceipt) {
        Page<ProductMaterialReceipt> productMaterialReceiptPage = new Page<>(page, rows);
        QueryWrapper<ProductMaterialReceipt> queryWrapper = new QueryWrapper<>();
        if (productMaterialReceipt.getPmrPId() != null) {
            queryWrapper.eq("pmr_p_id", productMaterialReceipt.getPmrPId());
        }
        if (!StringUtils.isBlank(productMaterialReceipt.getPmrPName())) {
            queryWrapper.or().like("pmr_p_name", productMaterialReceipt.getPmrPName());
        }
        if (productMaterialReceipt.getPmrSpecification() != null) {
            queryWrapper.or().eq("pmr_specification", productMaterialReceipt.getPmrSpecification());
        }
        Page<ProductMaterialReceipt> pageResult = productMaterialReceiptService.page(productMaterialReceiptPage, queryWrapper);
        return RestResult.ok(pageResult.getRecords(), pageResult.getTotal());
    }

    @RequestMapping("/addProductMaterialReceipt")
    @ApiOperation(value = "新增记录")
    public RestResult<ProductMaterialReceipt> addProductMaterialReceipt(
            @ApiParam(value = "新增项") ProductMaterialReceipt productMaterialReceipt){
        productMaterialReceiptService.save(productMaterialReceipt);
        return RestResult.ok("新增成功");
    }

    @RequestMapping("/updateProductMaterialReceipt")
    @ApiOperation(value = "修改记录")
    public RestResult<ProductMaterialReceipt> updateProductMaterialReceipt(
            @ApiParam(value = "修改项") ProductMaterialReceipt productMaterialReceipt){
        productMaterialReceiptService.updateById(productMaterialReceipt);
        return RestResult.ok("修改成功");
    }

    @RequestMapping("/saveOrUpdateProductMaterialReceipt")
    @ApiOperation(value = "新增或修改记录")
    public RestResult<ProductMaterialReceipt> saveOrUpdateProductMaterialReceipt(
            @ApiParam(value = "新增或修改项") ProductMaterialReceipt productMaterialReceipt){
        productMaterialReceiptService.saveOrUpdate(productMaterialReceipt);
        return RestResult.ok("操作成功");
    }

    @RequestMapping("/deleteProductMaterialReceipt")
    @ApiOperation(value = "删除记录", notes = "根据主键删除")
    public RestResult<ProductMaterialReceipt> deleteWarehouse(@ApiParam(value = "单据编号") Integer pmrId) {
        productMaterialReceiptService.removeById(pmrId);
        return RestResult.ok("删除成功");
    }
}

;;